package com.nba.system.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;






import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.alibaba.fastjson.JSONObject;
import com.nba.common.utils.ResponseUtils;
import com.nba.common.controller.BaseController;
import com.nba.common.exception.CustomException;
import com.nba.common.form.RequestDataForm;
import com.nba.common.form.ResponseDataForm;
import com.nba.common.framework.UserSessionData;
import com.nba.common.service.IService;
import com.nba.common.utils.DateUtil;
import com.nba.common.utils.Environment;
import com.nba.common.utils.FileUtils;
import com.nba.common.utils.MapUtils;
import com.nba.common.utils.PropertiesLoaderUtil;
import com.nba.common.web.httpobjects.MsgResult;

/**
 * @author Jesse 用户URL统一入口
 */
@Controller
@RequestMapping("/topic")
public class TopicController extends BaseController{
	private final String EXEC_SERVICE = "execService";
	private final String AJAX_SERVICE = "ajaxService";
	private final String QUERY_SERVICE = "queryService";
	
	/*@RequestMapping(value = "/uploadImg/{id}")
	public @ResponseBody Object uploadImg(@PathVariable String id, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		RequestDataForm requestDataForm = getRequestDataForm(id, request, response);
		Map<String, Object> urlSqlMap = requestDataForm.getUrlSqlMap();
		String service = MapUtils.getString(urlSqlMap, "SERVICE_NAME");
		if (null == service || "".equals(service)) {// service为空，直接跳转到页面
			return MapUtils.getString(urlSqlMap, "PAGE");
		} else {
			request.setAttribute(Environment.URL_SQL_MAP, urlSqlMap);// add
			WebApplicationContext ctx = WebApplicationContextUtils
					.getWebApplicationContext(request.getSession()
							.getServletContext());
			ResponseDataForm responseDataForm = ((IService) ctx
					.getBean(service)).service(requestDataForm);
			
			request.setAttribute("responseDataForm", responseDataForm);
			String fileid =  jdbcDao.queryForString("select max(att_id) lastId from sys_attachment_tab", null);
			Map<String, Object> m = new HashMap<String, Object>();
			m.put("error", 0);
			m.put("message", "上传成功");
			m.put("url", request.getContextPath() + "/topic/getFile/" + fileid);
			return m;
		}
	}	*/
	/**
	 * 返回json格式数据,返回类型可以是string，map，list，请将xmlresultType设置成正确的格式
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/ajax/{id}")
	public @ResponseBody
	MsgResult ajax(@PathVariable String id, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		RequestDataForm requestDataForm = getRequestDataForm(id, request, response);
		WebApplicationContext ctx = WebApplicationContextUtils
				.getWebApplicationContext(request.getSession()
						.getServletContext());
		return new MsgResult(((IService) ctx.getBean(this.AJAX_SERVICE)).service(requestDataForm)
				.getAjaxObject());
		
	}

	@RequestMapping(value = "/exec/{id}")
	public @ResponseBody
	MsgResult exec(@PathVariable String id, HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		RequestDataForm requestDataForm = getRequestDataForm(id, request, response);
		Map<String, Object> returnMap = new HashMap<String, Object>();
	    //保存操作日志
        Map<String, Object> urlSqlMap = requestDataForm.getUrlSqlMap();
        this.addOperateLog(request, id, MapUtils.getString(urlSqlMap, "TITLE"));
        //
		WebApplicationContext ctx = WebApplicationContextUtils
				.getWebApplicationContext(request.getSession()
						.getServletContext());
		ResponseDataForm responseDataForm = ((IService) ctx
				.getBean(this.EXEC_SERVICE)).service(requestDataForm);
		returnMap.put("success", true);
		returnMap.put("result", responseDataForm.getResult());
		returnMap.put("resultInfo", responseDataForm.getResultInfo());
		if (ResponseDataForm.FAULAIE == responseDataForm.getResult()) {// 异常信息
			returnMap.put("validate", responseDataForm.getErrorList());
		}
		return new MsgResult(returnMap);
	}

	/**
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/list/{id}")
	public @ResponseBody
	MsgResult list(@PathVariable String id, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		RequestDataForm requestDataForm = getRequestDataForm(id, request, response);
		WebApplicationContext ctx = WebApplicationContextUtils
				.getWebApplicationContext(request.getSession()
						.getServletContext());
		ResponseDataForm responseDataForm = ((IService) ctx
				.getBean(this.QUERY_SERVICE)).service(requestDataForm);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("count", responseDataForm.getPaginForm().getTotalCount());
		map.put("rows", responseDataForm.getPaginForm().getDataList());
		return new MsgResult(map);
	}

	/**
	 * 加载单条数据，返回json字符串,请使用ajaxservice,请将xml的resultType设置成map
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/load/{id}")
	public @ResponseBody
	MsgResult load(@PathVariable String id, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		RequestDataForm requestDataForm = getRequestDataForm(id, request, response);
		WebApplicationContext ctx = WebApplicationContextUtils
				.getWebApplicationContext(request.getSession()
						.getServletContext());
		ResponseDataForm responseDataForm = ((IService) ctx
				.getBean(this.AJAX_SERVICE)).service(requestDataForm);
		return new MsgResult(responseDataForm.getAjaxObject());
	}
	
	@RequestMapping(value = "/getSessionUser")
	public @ResponseBody
	MsgResult getSessionUser(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		UserSessionData userSession = (UserSessionData)request.getSession().getAttribute(Environment.SESSION_USER_LOGIN_INFO);
		return new MsgResult(userSession);
	}
	
		
	
	
	
}
